package cn.xspace.wnx.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xspace.android.xafcommon.view.RoundAngleImageView;

import java.util.List;

import cn.xspace.wnx.R;
import cn.xspace.library.bean.OrgDeptUserBean;
import com.xspace.android.xafcommon.util.HeadShowUtil;

/**
 * Created by liuchen on 2016/10/31.
 */

public class HeadAdapter extends BaseAdapter {
    private List<OrgDeptUserBean> userList;
    private static Context mContext;
    public static final int ADD_MEMBER_TYPE = 1;
    public static final int MIN_MEMBER_TYPE = -1;
    public static final int NORMAL_MEMBER_TYPE = 0;

    public HeadAdapter(Context context, List<OrgDeptUserBean> dataList) {
        mContext = context;
        userList = dataList;
    }

    @Override
    public int getCount() {
        return userList.size();
    }

    @Override
    public OrgDeptUserBean getItem(int position) {
        return userList.get(position);
    }

    @Override
    public int getItemViewType(int position) {

        if (getItem(position).isMinButton) {
            return MIN_MEMBER_TYPE;
        }
        if (getItem(position).isAddButton) {
            return ADD_MEMBER_TYPE;
        }
        return NORMAL_MEMBER_TYPE;
    }

    @Override
    public long getItemId(int position) {
        return userList.get(position).hashCode();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        switch (getItemViewType(position)) {
            case NORMAL_MEMBER_TYPE:
                OrgDeptUserBean bean = userList.get(position);
                ViewHolder viewHolder;
                if (convertView == null) {
                    viewHolder = new ViewHolder();
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.grid_item, null);
                    viewHolder.findView(convertView);
                    convertView.setTag(viewHolder);
                } else {
                    viewHolder = (ViewHolder) convertView.getTag();
                }
                viewHolder.setData(viewHolder, bean);
                break;
            case ADD_MEMBER_TYPE:
                convertView = LayoutInflater.from(mContext).inflate(R.layout.grid_item_member_add, null);
                break;
            case MIN_MEMBER_TYPE:
                convertView = LayoutInflater.from(mContext).inflate(R.layout.grid_item_member_min, null);
                break;

        }
        return convertView;
    }

    static class ViewHolder {
        TextView nameTxt;
        ImageView logoImg;

        public void findView(View view) {
            nameTxt = (TextView) view.findViewById(R.id.txt_name);
            logoImg = (RoundAngleImageView) view.findViewById(R.id.image_logo);
        }

        public void setData(ViewHolder viewHolder, OrgDeptUserBean bean) {
            viewHolder.nameTxt.setText(bean.user_name);


            HeadShowUtil.headShow(mContext, bean.user_logo, bean.user_sex, viewHolder.logoImg, HeadShowUtil.HeadType.UserLogo);
//            if (!TextUtils.isEmpty(bean.user_logo)) {
//                Picasso.with(mContext)
//                        .load(bean.user_logo)
//                        .placeholder(R.drawable.icon_header_default)
//                        .error(R.drawable.icon_header_default)
//                        .into(viewHolder.logoImg);
//            } else {
//                viewHolder.logoImg.setImageResource(R.drawable.icon_header_default);
//            }

        }
    }
}
